package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.TopicDao;
import com.lostandfound.entity.Topic;

/**  
* 
*  
* @date 2018年3月11日
*/
@Repository("topicDao")
public class TopicDaoImpl implements TopicDao {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public Serializable save(Topic topic) {
		logger.info("发表了一个帖子");
		return sessionFactory.getCurrentSession().save(topic);
	}

	@Override
	public void update(Topic topic) {
		logger.info("修改了帖子");
		sessionFactory.getCurrentSession().update(topic);
	}

	@Override
	public void delete(Topic topic) {
		logger.info("删除了帖子");
		sessionFactory.getCurrentSession().delete(topic);
	}

	@Override
	public Topic selectByPrimary(Long id) {
		return (Topic)sessionFactory.getCurrentSession().load(Topic.class, id);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List listTopicByPage(Map<String, Object> pagemap) {
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Topic t where 1=1 ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and t.user.userCode like '"
						+(String)pagemap.get("userCode")+ "%' ";
			}
			if(pagemap.get("pname") != null && !((String)pagemap.get("pname")).equals("")){
				hql = hql + "and t.plate.pname like '"
						+ (String)pagemap.get("pname") +"%' ";
			}
			if(pagemap.get("tname") != null && !((String)pagemap.get("tname")).equals("")){
				hql = hql + "and t.tname like '%"
						+ (String)pagemap.get("tname") +"%' ";
			}
			if(pagemap.get("tstatus") != null && !((String)pagemap.get("tstatus")).equals("")){
				hql = hql + "and t.tstatus = "
						+ (String)pagemap.get("tstatus");
			}else{
				hql = hql + "and (t.tstatus = '0' or t.tstatus = '1' or t.tstatus = '2') ";
			}
			if(pagemap.get("item_type") != null && !((String)pagemap.get("item_type")).equals("")){
				hql = hql + "and t.item_type = '"
						+ (String)pagemap.get("item_type") +"' ";
			}
			if(pagemap.get("lost_address") != null && !((String)pagemap.get("lost_address")).equals("")){
				hql = hql + "and t.lost_address = '"
						+ (String)pagemap.get("lost_address") +"' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("")){
				hql = hql + "and t.t_create_time BETWEEN '"
						+ pagemap.get("mindate") +"' AND '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		int page = Integer.parseInt((String)pagemap.get("page"));
		int size = Integer.parseInt((String)pagemap.get("size"));
		Query query = session.createQuery(hql)
				.setFirstResult((page-1)*size)
				.setMaxResults(size);
		List<?> list = query.list();

		return list;
	}

	@Override
	public int Count(Map<String, Object> pagemap) {
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from Topic t where 1=1 ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and t.user.userCode like '"
						+(String)pagemap.get("userCode")+ "%' ";
			}
			if(pagemap.get("pname") != null && !((String)pagemap.get("pname")).equals("")){
				hql = hql + "and t.plate.pname like '"
						+ (String)pagemap.get("pname") +"%' ";
			}
			if(pagemap.get("tstatus") != null && !((String)pagemap.get("tstatus")).equals("")){
				hql = hql + "and t.tstatus = '"
						+ (String)pagemap.get("tstatus") + "' ";
			}else{
				hql = hql + "and (t.tstatus = '0' or t.tstatus = '1' or t.tstatus = '2') ";
			}
			if(pagemap.get("item_type") != null && !((String)pagemap.get("item_type")).equals("")){
				hql = hql + "and t.item_type = '"
						+ (String)pagemap.get("item_type") +"' ";
			}
			if(pagemap.get("lost_address") != null && !((String)pagemap.get("lost_address")).equals("")){
				hql = hql + "and t.lost_address = '"
						+ (String)pagemap.get("lost_address") +"' ";
			}
			if(pagemap.get("mindate") != null && 
					!pagemap.get("mindate").equals("") && 
					pagemap.get("maxdate") != null && 
					!pagemap.get("maxdate").equals("")){
				hql = hql + "and t.t_create_time BETWEEN '"
						+ pagemap.get("mindate") +"' AND '"
						+ pagemap.get("maxdate") + "'";
			}
		}
		Query query = session.createQuery(hql);

		return ((Number) query.iterate().next())
	  	         .intValue();
	}

	/**
	 * 执行批量删除操作
	 */
	@Override
	public void deleteTopicByBranch(Long[] topics) {
		String hql = "";
		for(int i=0;i<topics.length;i++) {
            if(i==0) {
                hql = "t.tid="+topics[i];
            } else {
                hql =hql + " or t.tid="+topics[i];
            }
        }
		if(!hql.equals("")){
	        Session session= sessionFactory.getCurrentSession();
	        Query q= session.createQuery("update Topic t set t.tstatus = '3' where " + hql);
	        q.executeUpdate();
			logger.info("批量删除帖子" + topics);
		}
	}

	@Override
	public void deleteTopicByUser(Long uid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update Topic t set t.tstatus = '3' where t.user.id = ? ";
		session.createQuery(hql).setParameter(0, uid)
				.executeUpdate();
		logger.info("删除某个用户下的帖子" + uid);
	}

	@Override
	public void deleteTopicByPlate(Long pid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update Topic t set t.tstatus = '3' where t.plate.pid = ? ";
		session.createQuery(hql).setParameter(0, pid)
				.executeUpdate();
		logger.info("删除某个板块下的帖子" + pid);
	}

}
